package com.example.littleken.secret.Activitys;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.littleken.secret.Config;
import com.example.littleken.secret.Net.Comment;
import com.example.littleken.secret.Net.GetComment;
import com.example.littleken.secret.Net.PubComment;
import com.example.littleken.secret.R;
import com.example.littleken.secret.tools.MD5Tool;

import java.util.List;

/**
 * 本类实现了主界面的具体逻辑
 */
public class AtyMessage extends ListActivity {
    private String phone_md5;
    private String msg;
    private String msgId;
    private TextView tvMessage;
    private String token;
    private EditText etComment;
    private AtyMessageCommentListAdaoter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_aty_message);
        //实例化适配器
        adapter = new AtyMessageCommentListAdaoter(this);
        //设置适配器
        setListAdapter(adapter);

        //数据初始化
        tvMessage = (TextView) findViewById(R.id.tv_message);
        etComment = (EditText) findViewById(R.id.etComment);
        Intent data = getIntent();
        phone_md5 = data.getStringExtra(Config.KEY_PHONE_MD5);
        msg = data.getStringExtra(Config.KEY_MSG);
        msgId = data.getStringExtra(Config.KEY_MSG_ID);
        token = data.getStringExtra(Config.KEY_TOKEN);
        tvMessage.setText(msg);

        getComments();
        //实现消息发布
        findViewById(R.id.btn_sendCommnet).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(etComment.getText())) {
                    Toast.makeText(AtyMessage.this, R.string.comment_can_not_be_null, Toast.LENGTH_SHORT).show();
                    return;
                }
                final ProgressDialog pd = ProgressDialog.show(AtyMessage.this, getResources().getString(R.string.connecting), getResources().getString(R.string.connecting_to_server));
                new PubComment(MD5Tool.md5(Config.getCachePhoneNum(AtyMessage.this)), token,  msgId, new PubComment.SuccessCallback() {
                    @Override
                    public void onSuccess() {
                        pd.dismiss();
                        etComment.setText("");
                        getComments();
                    }
                }, new PubComment.FailCallback() {
                    @Override
                    public void onFail(int errorCode) {
                        pd.dismiss();

                        if (errorCode==Config.RESULT_STATUS_INVALID_TOKEN) {
                            startActivity(new Intent(AtyMessage.this,AtyLogin.class));
                            finish();
                        }else{
                            Toast.makeText(AtyMessage.this, R.string.fail_to_get_comment, Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
    }

    /**
     * 获取评论方法
     */
    private void getComments() {
        final ProgressDialog pd = ProgressDialog.show(this, getResources().getString(R.string.connecting), getResources().getString(R.string.connecting_to_server));
        new GetComment(phone_md5, token, msgId, 1, 20, new GetComment.SuccessCallback() {

            @Override
            public void onSuccess(String msgId, int page, int perpage,
                                  List<Comment> comments) {
                pd.dismiss();
                adapter.clear();
                adapter.addAll(comments);
            }
        }, new GetComment.FailCallback() {

            @Override
            public void onFail(int errorCode) {
                pd.dismiss();

                if (errorCode == Config.RESULT_STATUS_INVALID_TOKEN) {
                    startActivity(new Intent(AtyMessage.this, AtyLogin.class));
                    finish();
                } else {
                    Toast.makeText(AtyMessage.this, R.string.fail_to_get_comment, Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}
